<script type="text/javascript">
$(function(){
	//调整税率
	$(".add_cus").click(function(){
		var rate = $(".add_cus").attr('now');
		$('#customeModal').modal();
		$('#now_rate').val(rate);
		var date = "{$today}";
		$('.change_date').val(date);
		var old_date = "{$nowdate[0]}";
		$('#now_rate_time').val(old_date);
	});
	//补差
	$(".payment").click(function(){
		$('#paymentModal').modal();
		var pname = $(this).attr('pname');
		var pid = $(this).attr('pid');
		$('#pname').val(pname);
		var date = "{$today}";
		$('.payment_date').val(date);
		$('#pid').val(pid);
	});
	//基础管理分摊方式
	$(".add_base").click(function(){
		var rate = $(this).attr('now');
		var rid = $(this).attr('rid');
		var pid = $(this).attr('pid');
		var rate_type = $(this).attr('rate_type');
		var projectname = $(this).attr('pname');
		$('#a').click();
		$('#monthModal').modal();
		$("#headname").text('基础管理收入分摊方式');
		$('#rate_month').val(rate);
		$('#rate').val(rid);
		$('#projectname').val(projectname);
		$('#rate_type').val(rate_type);
		$('#project_id').val(pid);
		var tr_num = $(".month").find("tr").length;
		if(tr_num>5){
			$(".month tr:last").remove();      //移除最后一行
			$(".month tr:last").remove();      //移除次最后一行
		}
	});
	//基础管理查看
	$(".base_detail").click(function(){
		var $tables = $('.month');
		var rid = $(this).attr("rid");
		var projectname = $(this).attr('pname');
		modal = $("#monthModal");
		$.ajax({
			type:"POST",
			async:false,
			dataType:"json",
			data:{"id":rid},
			url:"__APP__/Crm/getRuleByIncomeId",
			success:function(data){
				var it = data.income_type;
				if(it==1){
					modal.find('.radio-item input[name="apportion_type"]').eq(0).click();
				}else{
					modal.find('.radio-item input[name="apportion_type"]').eq(1).click();
				}
				modal.find("#rate_month").val(data.rate);
				modal.find("#projectname").val(projectname);
				$(".month tr:last").remove();
				$(".month tr:last").remove();
				var addtr = data.rule_info;
				$tables.append(addtr);
				$('.btn-card-month').hide();
				$('.btn-month-add').hide();
				$('.btn-month-del').hide();
				modal.modal();
			}
		});
		$("#headname").text('基础管理收入分摊方式');
	});
	//浮动查看
	$(".float_detail").click(function(){
		var $tables = $('.month');
		var rid = $(this).attr("rid");
		var projectname = $(this).attr('pname');
		modal = $("#monthModal");
		$.ajax({
			type:"POST",
			async:false,
			dataType:"json",
			data:{"id":rid},
			url:"__APP__/Crm/getRuleByIncomeId",
			success:function(data){
				var it = data.income_type;
				if(it==1){
					modal.find('.radio-item input[name="apportion_type"]').eq(0).click();
				}else{
					modal.find('.radio-item input[name="apportion_type"]').eq(1).click();
				}
				modal.find("#rate_month").val(data.rate);
				modal.find("#projectname").val(projectname);
				var tr_num = $(".month").find("tr").length;
				$(".month tr:last").remove();
				$(".month tr:last").remove();
				var addtr = data.rule_info;
				$tables.append(addtr);
				$('.btn-card-month').hide();
				$('.btn-month-add').hide();
				$('.btn-month-del').hide();
				modal.modal();
			}
		});
		$("#headname").text('浮动收入分摊方式');
	});
	//浮动分摊方式
	$(".add_float").click(function(){
		var rate = $(this).attr('now');
		var rid = $(this).attr('rid');
		var pid = $(this).attr('pid');
		var rate_type = $(this).attr('rate_type');
		var projectname = $(this).attr('pname');
		$('#b').click();
		$('#monthModal').modal();
		$("#headname").text('浮动收入分摊方式');
		$('#rate_month').val(rate);
		$('#rate').val(rid);
		$('#projectname').val(projectname);
		$('#rate_type').val(rate_type);
		$('#project_id').val(pid);
		var tr_num = $(".month").find("tr").length;
		if(tr_num>5){
			$(".month tr:last").remove();      //移除最后一行
			$(".month tr:last").remove();      //移除次最后一行
		}
	});
	//新增公式month
	$(".btn-month-add").click(function(){
		var check_type = $('input:radio[name="apportion_type"]:checked').val();
		var tables = $('.month');
		var tr_num = $(".month").find("tr").length;
		var order_hang=5;
		var order_cha=3;
		var num = tr_num-((tr_num-order_hang)/2+order_cha);//差值计算公式
		var rule1 = "<literal>onclick=\"WdatePicker({maxDate:'#F{$dp.$D(\\'end"+num+"\\')}',startDate:'#F{$dp.$D(\\'end"+num+"\\')}'})\"</literal>";
		var rule2 = "<literal>onclick=\"WdatePicker({minDate:'#F{$dp.$D(\\'start"+num+"\\')}',startDate:'#F{$dp.$D(\\'start"+num+"\\')}'})\"</literal>";
		var addtr = $("<tr><td class='fbold' width='200'><strong class='red'>*</strong>确认日期</td>"+
"<td><input id='start"+num+"' type='text' name='start_date[]' class='form-control Wdate required-text1 w100 start_date' "+rule1+" readonly><span class='error-item'>必填项</span>"+"<label class='fuhao'>~</label><input id='end"+num+"' type='text' name='end_date[]' class='form-control Wdate required-text1 w100 end_date' "+rule2+" readonly><span class='error-item'>必填项</span>"+"</td></tr>"+"<tr><td class='fbold income_name'><strong class='red'>*</strong>基础管理费</td>"+"<td><input type='text' name='amount[]' class='form-control required-text1 rate' onkeyup='num(this);'>"+"<span class='error-item'>必填项</span></tr>");
		var addtr2 = $("<tr><td class='fbold' width='200'><strong class='red'>*</strong>确认日期</td>"+
"<td><input type='text' name='start_date[]' class='form-control Wdate required-text1 w100 start_date' onclick='WdatePicker()' readonly><span class='error-item'>必填项</span>"+"<label class='fuhao'>~</label><input type='text' name='end_date[]' class='form-control Wdate required-text1 w100 end_date' onclick='WdatePicker()' readonly><span class='error-item'>必填项</span>"+"</td></tr>"+"<tr><td class='fbold income_name'><strong class='red'>*</strong>浮动收益</td>"+"<td><input type='text' name='amount[]' class='form-control required-text1 rate' onkeyup='num(this);'>"+"<span class='error-item'>必填项</span></tr>");
		if(check_type==1){
			addtr.appendTo(tables);
			$('.end_date').show();
			$('.end_date').addClass("required-text1");
			$('.fuhao').show();
		}else if(check_type==2){
			addtr2.appendTo(tables);
			$('.end_date').hide();
			$('.end_date').removeClass("required-text1");
			$('.fuhao').hide();
		}
	});
	//减少公式
	$(".btn-month-del").click(function(){
		$(this).nextAll('.error-item').hide();
		var tr_num = $(".month").find("tr").length;
		if(tr_num>5){
			$(".month tr:last").remove();      //移除最后一行
			$(".month tr:last").remove();      //移除次最后一行
		}else{
			$(this).nextAll('.error-item').show();
		}
	});
	//切换分摊方式
	$('input:radio[name="apportion_type"]').change(function() {
		if (this.value == '1') {
			$('.income_name').html('<strong class="red">*</strong>基础管理费');
			$('.end_date').show();
			$('.end_date').addClass("required-text1");
			$(".required-text1").nextAll('.error-item').hide();
			$('.fuhao').show();
		}
		else if (this.value == '2') {
			$('.income_name').html('<strong class="red">*</strong>浮动收益');
			$('.end_date').hide();
			$('.end_date').removeClass("required-text1");
			$(".required-text1").nextAll('.error-item').hide();
			var patt=/end/g;
			$(".month tr input").each(function(){
				var name =$(this).attr("id");
				if(patt.test(name)){
					$("#"+name).val('');
				}
			}) 
			$('.fuhao').hide();
		}
	});
	//表单验证-调整税率
	$(".btn-card").click(function(){
		var tag = true;
		$(".required-text").each(function(){
			var $val = $(this).val();
			if($val=="" || /^\s+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		var old = $('#now_rate').val();
		var new_rate = $('.rate').val();
		if(old==new_rate){
			$(".rate").next(".error-item").text("税率新老值一致无意义!!!").show();
			tag = false;
		}
		if(tag){
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:$(".add-custome-info1").serialize()+"&old_rate="+old+"&action=rate",
				url:"__APP__/Crm/income_rule_doadd",
				success:function(data){
					success_pop(data.status);
				}
			});
		}
	});
	
	//表单验证--分摊方式
	$(".btn-card-month").click(function(){
		var tag = true;
		$(".required-text1").each(function(){
			var $val = $(this).val();
			if($val=="" || /^\s+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		if(tag){
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:$(".add-custome-month").serialize()+"&action=income",
				url:"__APP__/Crm/income_rule_doadd",
				success:function(data){
					success_pop(data.status);
				}
			});
		}
	});
	//表单验证-税调补差
	$(".btn-payment-card").click(function(){
		var tag = true;
		$(".required-text3").each(function(){
			var $val = $(this).val();
			if($val=="" || /^\s+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		if(tag){
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:$(".add-custome-payment").serialize()+"&action=payment",
				url:"__APP__/Crm/income_rule_doadd",
				success:function(data){
					if(data.status==1){
						success_pop(data.status);
					}else{
						$("#paymentModal").find(".ajax_error_tip2").text(data.msg).show();
					}
				}
			});
		}
	});
	$(".required-text").focus(function () {
		$(this).nextAll('.error-item').hide();
	});
	$(".add-custome-month").on('focus','.required-text1',function(){
		$(this).nextAll('.error-item').hide();
	});
	//税率调整--取消
	$("#customeModal").on('hidden.bs.modal',function(){
		$(this).find(".error-item").hide();
		$(this).find(".rate").val("");
	});
	//变更收入方式--month
	$("#monthModal").on('hidden.bs.modal',function(){
		<!-- console.log(8); -->
		<!-- $('.radio-item input').attr('checked',false).removeAttr('checked'); -->
		var tr_num = $(".month").find("tr").length;
		if(tr_num>5){
			$(".month tr:last").remove();      //移除最后一行
			$(".month tr:last").remove();      //移除次最后一行
		}
		$(this).find(".error-item").hide();
		$(this).find(".required-text1").val("");
		$('.btn-card-month').show();
		$('.btn-month-add').show();
		$('.btn-month-del').show();
	});
	$("#paymentModal").on('hidden.bs.modal',function(){
		$(this).find(".error-item").hide();
		$(this).find(".required-text3").val("");
	});
});

function num(obj){
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
	obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
}

</script>